/*
 * Copyright (C) 2012 Martin Řehánek
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mzk.funnylib.api.builders;

import cz.mzk.funnylib.dao.db.BookCheckDAOImpl;
import cz.mzk.funnylib.entities.BookCheck;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Martin Řehánek
 */
public class BookChecksBuilder extends AbstractBuilder{

    public String buildBookChecksOfUser(long userId) {
        //TODO: tahat z databaze
//        List<BookCheck> checks = new ArrayList<BookCheck>();
//        for (int i = 0; i < 5; i++) {
//            checks.add(mockBookCheck(i, userId));
//        }
        BookCheckDAOImpl dao = new BookCheckDAOImpl(getJdbcTemplate());
        List<BookCheck> checks = dao.getBookChecksForUser(userId);
        
        return buildBookChecksOfUser(checks, userId);
    }
    
    public String buildBookChecksOfUser(List<BookCheck> checks, long userId) {
        StringBuilder result = new StringBuilder();
        result.append("{");
        result.append("\"").append("userId").append("\"").append(":");
        result.append("\"").append(userId).append("\"");
        result.append(",\n");
        result.append("\"").append("bookChecks").append("\"").append(":");
        result.append("[\n");
        for (int i = 0; i < checks.size(); i++) {
            result.append(buildBookCheck(checks.get(i)));
            if (i != checks.size() - 1) {
                result.append(",\n");
            }
        }
        result.append("\n]");
        result.append("\n}");
        return result.toString();
    }
    
    public String buildBookCheck(BookCheck check) {
        StringBuilder result = new StringBuilder();
        result.append("{");
        result.append("\"").append("id").append("\"").append(":");
        result.append("\"").append(check.getId()).append("\"");
        result.append(",");
        result.append("\"").append("userId").append("\"").append(":");
        result.append("\"").append(check.getUserId()).append("\"");
        result.append(",");
        result.append("\"").append("libraryId").append("\"").append(":");
        result.append("\"").append(check.getLibraryId()).append("\"");
        result.append(",");
        result.append("\"").append("name").append("\"").append(":");
        result.append("\"").append(check.getName()).append("\"");
        result.append(",");
        result.append("\"").append("isbn").append("\"").append(":");
        result.append("\"").append(check.getIsbn()).append("\"");
        result.append(",");
        result.append("\"").append("date").append("\"").append(":");
        result.append("\"").append(check.getDate()).append("\"");
        result.append("}");
        return result.toString();
    }
    
    public BookCheck mockBookCheck(long id, long userId) {
        BookCheck result = new BookCheck();
        result.setId(id);
        result.setUserId(userId);
        result.setLibraryId(Long.valueOf(456));
        result.setIsbn("isbn123");
        result.setDate(new Date());
        result.setName("Babička");
        return result;
    }
}
